package com.ruoyi.web.controller.set;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.SetProductCatalog;
import com.ruoyi.system.service.ISetProductCatalogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 总成大类Controller
 *
 * @author ruoyi
 * @date 2022-11-21
 */
@RestController
@Api(tags = "总成大类信息")
@RequestMapping("/set/productCatalog")
public class SetProductCatalogController extends BaseController
{
    @Autowired
    private ISetProductCatalogService setProductCatalogService;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询总成大类列表
     */
    //@PreAuthorize("@ss.hasPermi('set:productCatalog:list')")
    @ApiOperation(value = "查询总成大类列表")
    @GetMapping("/list")
    public TableDataInfo list(@ApiParam(value = "总成大类参数", required = false) SetProductCatalog setProductCatalog)
    {
        startPage();
        List<SetProductCatalog> list = setProductCatalogService.selectSetProductCatalogList(setProductCatalog);
        return getDataTable(list);
    }

    /**
     * 查询总成大类下拉接口
     */
    //@PreAuthorize("@ss.hasPermi('set:productCatalog:interface')")
    @ApiOperation(value = "查询总成大类下拉接口")
    @GetMapping("/interface")
    public AjaxResult interfaces(@ApiParam(value = "总成大类参数", required = false) SetProductCatalog setProductCatalog)
    {

        List<SetProductCatalog> list = setProductCatalogService.selectSetProductCatalogList(setProductCatalog);
        return AjaxResult.success(list);
    }

    /**
     * 导出总成大类列表
     */
    //@PreAuthorize("@ss.hasPermi('set:productCatalog:export')")
    @Log(title = "总成大类", businessType = BusinessType.EXPORT)
    @ApiOperation(value = "导出总成大类列表")
    @GetMapping("/export")
    public AjaxResult export(HttpServletResponse response, @ApiParam(value = "总成大类参数", required = false) SetProductCatalog setProductCatalog)
    {
        List<SetProductCatalog> list = setProductCatalogService.selectSetProductCatalogList(setProductCatalog);
        ExcelUtil<SetProductCatalog> util = new ExcelUtil<SetProductCatalog>(SetProductCatalog.class);
        return util.exportExcel( list, "总成大类数据");
    }

    /**
     * 导入总成大类列表
     */
    @Log(title = "总成大类", businessType = BusinessType.IMPORT)
    //@PreAuthorize("@ss.hasPermi('set:productCatalog:import')")
    @ApiOperation(value = "导入总成大类列表")
    @PostMapping("/importData")
    public AjaxResult importData(@ApiParam(value = "导入文件", required = true) MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<SetProductCatalog> util = new ExcelUtil<SetProductCatalog>(SetProductCatalog.class);
        List<SetProductCatalog> productCatalogList = util.importExcel(file.getInputStream());
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        String operName = loginUser.getUsername();
        String message = setProductCatalogService.importProductCatalog(productCatalogList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    /**
     * 导入模板
     */
    @ApiOperation(value = "导入模板")
    @GetMapping("/importTemplate")
    public AjaxResult importTemplate()
    {
        List<SetProductCatalog> list = setProductCatalogService.selectSetProductCatalogList(new SetProductCatalog());
        ExcelUtil<SetProductCatalog> util = new ExcelUtil<SetProductCatalog>(SetProductCatalog.class);
        return util.importTemplateExcel(list,"总成大类数据");
    }

    /**
     * 获取总成大类详细信息
     */
    //@PreAuthorize("@ss.hasPermi('set:productCatalog:query')")
    @ApiOperation(value = "获取总成大类详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@ApiParam(value = "总成大类id", required = true) @PathVariable("id") Long id)
    {
        return AjaxResult.success(setProductCatalogService.selectSetProductCatalogById(id));
    }

    /**
     * 新增总成大类
     */
    //@PreAuthorize("@ss.hasPermi('set:productCatalog:add')")
    @Log(title = "总成大类", businessType = BusinessType.INSERT)
    @ApiOperation(value = "新增总成大类")
    @PostMapping
    public AjaxResult add(@ApiParam(value = "总成大类实体类", required = true) @RequestBody SetProductCatalog setProductCatalog)
    {
        if (UserConstants.NOT_UNIQUE.equals(setProductCatalogService.checkCodeUnique(setProductCatalog)))
        {
            return AjaxResult.error("新增总成大类'" + setProductCatalog.getProductCatalogName()+ "'失败，总成大类编码已存在");
        }
        else if (UserConstants.NOT_UNIQUE.equals(setProductCatalogService.checkNameUnique(setProductCatalog)))
        {
            return AjaxResult.error("新增总成大类'" + setProductCatalog.getProductCatalogName() + "'失败，总成大类名称已存在");
        }
        return toAjax(setProductCatalogService.insertSetProductCatalog(setProductCatalog));
    }

    /**
     * 修改总成大类
     */
    //@PreAuthorize("@ss.hasPermi('set:productCatalog:edit')")
    @Log(title = "总成大类", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改总成大类")
    @PutMapping
    public AjaxResult edit(@ApiParam(value = "总成大类实体类", required = true) @RequestBody SetProductCatalog setProductCatalog)
    {
        if (UserConstants.NOT_UNIQUE.equals(setProductCatalogService.checkCodeUnique(setProductCatalog)))
        {
            return AjaxResult.error("修改总成大类'" + setProductCatalog.getProductCatalogName()+ "'失败，总成大类编码已存在");
        }
        else if (UserConstants.NOT_UNIQUE.equals(setProductCatalogService.checkNameUnique(setProductCatalog)))
        {
            return AjaxResult.error("修改总成大类'" + setProductCatalog.getProductCatalogName() + "'失败，总成大类名称已存在");
        }
        return toAjax(setProductCatalogService.updateSetProductCatalog(setProductCatalog));
    }

    /**
     * 删除总成大类
     */
    //@PreAuthorize("@ss.hasPermi('set:productCatalog:remove')")
    @Log(title = "总成大类", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除总成大类")
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@ApiParam(value = "总成大类id数组", required = true) @PathVariable Long[] ids)
    {
        return toAjax(setProductCatalogService.deleteSetProductCatalogByIds(ids));
    }
}
